package com.sun.StudentInfo.handler;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.sun.StudentInfo.pojo.Course;
import com.sun.StudentInfo.pojo.CoursePlan;
import com.sun.StudentInfo.pojo.Student;
import com.sun.StudentInfo.pojo.Teacher;
import com.sun.StudentInfo.service.CoursePlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.sun.StudentInfo.service.CourseService;




@Controller
@RequestMapping("/CoursePlanHandler")
public class CoursePlanHandler {

	@Autowired
    CoursePlanService coursePlanService;
	@Autowired
    CourseService courseService;

	//添加课程方案
	@PostMapping("/doaddcouplan/{tid}")
	public ModelAndView addCoursePlan(Model model, HttpSession httpSession,
									  String courseclass,
									  String courseweek,
									  String coursetime,
									  String cid,
									  String tid,
									  String classroom,
									  int credits,
									  int period,
									  int totalnum) {

		CoursePlan coursePlan = new CoursePlan();
		coursePlan.setCourseclass(courseclass);
		coursePlan.setCourseweek(courseweek);
		coursePlan.setCoursetime(coursetime);
		coursePlan.setCid(cid);
		coursePlan.setTid(tid);
		coursePlan.setClassroom(classroom);
		coursePlan.setCredits(credits);
		coursePlan.setPeriod(period);
		coursePlan.setTotalnum(totalnum);

		CoursePlan cp = coursePlanService.getCoursePlanByCid(cid);
		if (cp != null) {
			coursePlanService.modifyCoursePlan(coursePlan);
			return new ModelAndView(new RedirectView("/StudentInfo/TeacherHandler/managecou/{tid}/1"));
		}else if (coursePlanService.insertCoursePlan(coursePlan) != 0) {
			model.addAttribute("coursePlan", coursePlan);
			System.out.println(coursePlan);
			
			return new ModelAndView(new RedirectView("/StudentInfo/TeacherHandler/managecou/{tid}/1"));
			//return "success";

		} else {
			return new ModelAndView(new RedirectView("/StudentInfo/fail.jsp"));
		}

	}

	//查询课程方案
	@RequestMapping(value = "/querycouplan/{cid}", method = RequestMethod.GET)
	public String deleteStudent(@PathVariable(value = "cid") String cid, Model model, HttpSession session, HttpServletRequest request) {
		
		if(coursePlanService.existsCoursePlan(cid) != null) {
			request.setAttribute("msg", "该课程已经有老师代课，无法选择此课程！");
			return "fail";
		}
		model.addAttribute("cid", cid);
		System.out.println(cid);
		return "teacher/doAddCou";

	}

	//跳转
	@RequestMapping("/addquery")
	public String adStudent(HttpServletRequest request, Model model,
							@RequestParam(value = "pageNo", required = false) Integer pageNo,
							@RequestParam(value = "pageSize", required = false) Integer pageSize,
							@RequestParam(value = "searchType", required = false) String searchType,
							@RequestParam(value = "value", required = false) String value) {

		// 开启分页查询
		Map<String, Integer> pageNoAndPageSize = formatPageNoAndPageSize(pageNo, pageSize);
		PageHelper.startPage(pageNoAndPageSize.get("pageNo"), pageNoAndPageSize.get("pageSize"));

		request.setAttribute("searchType", searchType);
		request.setAttribute("value", value);

		List<Course> courseList = courseService.selectTeacherCourses(searchType, value);
		pageIn(model, courseList);
		request.setAttribute("courseList", courseList);
		return "teacher/addCou";
	}

	//查询所有课程方案
	@RequestMapping(value = "/queryy/{pn}", method = RequestMethod.GET)
	public String redirect(@RequestParam("serc") String serc, @RequestParam("condition") String condition,
			Model model, HttpSession httpSession,@PathVariable(value = "pn") String pn,HttpServletRequest request,@RequestParam("courseType") String courseType) {

		int no = Integer.parseInt(pn);
		List<Course> courseList = new ArrayList<Course>();
		PageHelper.startPage(no, 5);
		request.setAttribute("serc", serc);
		request.setAttribute("condition", condition);
		request.setAttribute("courseType", courseType);
		
		if (serc.equals("all")) {

			pageIn(model, courseList);
			httpSession.setAttribute("courseList", courseList);
			System.out.println(courseList);
			return "teacher/addCou";

		} else if (serc.equals("sid")) {

			pageIn(model, courseList);
			httpSession.setAttribute("courseList", courseList);
			System.out.println("sid");

			return "teacher/addCou";

		} else if (serc.equals("nam")) {
			pageIn(model, courseList);
			httpSession.setAttribute("courseList", courseList);
			System.out.println(courseList);
			System.out.println("cla");
			return "teacher/addCou";

		} else if (serc.equals("col")) {
			pageIn(model, courseList);
			httpSession.setAttribute("courseList", courseList);
			System.out.println(courseList);
			System.out.println("col");
			return "teacher/addCou";

		} else if (serc.equals("type")) {
			pageIn(model, courseList);
			httpSession.setAttribute("courseList", courseList);
			System.out.println(courseList);
			System.out.println("pro");
			return "teacher/addCou";

		} else {

			pageIn(model, courseList);
			httpSession.setAttribute("courseList", courseList);
			System.out.println(courseList);
			return "teacher/addCou";

		}

	}

	//修改课程方案
	@RequestMapping("/modicouplan/{cid}")
	public String modiCouPlan(@PathVariable(value = "cid") String cid, Model model,
			HttpSession httpSession) {
		
		List<CoursePlan> cPlanList = coursePlanService.getByCoursePlanCourseclass(1,10,cid);

		if (cPlanList != null && cPlanList.size() > 0) {
			httpSession.setAttribute("cplan", cPlanList.get(0));

			CoursePlan coursePlan = cPlanList.get(0);
			String coursetime = coursePlan.getCoursetime();
			Map<Integer, String> selectionMap = new HashMap<>();
			String[] split = coursetime.split("，");
			for (int i = 0; i < split.length; i++) {
				selectionMap.put(i, split[i]);
			}
			httpSession.setAttribute("selectionMap", selectionMap);

			System.out.println(cPlanList);
			return "teacher/modCouPlan";

		} else {
			return "fail";
		}

	}

	//修改
	@RequestMapping("/moditycouplan/{tid}")
	public ModelAndView modityCouPlan(@PathVariable(value = "tid") String tid, CoursePlan coursePlan) {

		if (coursePlanService.modifyCoursePlan(coursePlan) != 0) {
			return new ModelAndView(new RedirectView("/StudentInfo/TeacherHandler/managecou/{tid}/1"));
		} else {

			return new ModelAndView(new RedirectView("/StudentInfo/fail.jsp"));
		}
	}

	//删除
	@RequestMapping("/delcouplan/{cid}/{tid}")
	public ModelAndView modityCouPlan(@PathVariable(value = "cid") String cid,
			@PathVariable(value = "tid") String tid, HttpSession httpSession) {
		

		if (coursePlanService.deleteCoursePlan(cid) != 0) {
			
			httpSession.removeAttribute("couList");
			httpSession.removeAttribute("coursePlanList");
			return new ModelAndView(new RedirectView("/StudentInfo/TeacherHandler/managecou/{tid}/1"));
		} else {

			return new ModelAndView(new RedirectView("/StudentInfo/fail.jsp"));
		}
	}

	/**
	 * 获取session中的老师
	 */
	private Teacher getTeacher(HttpServletRequest request) {
		HttpSession session = request.getSession();
		return (Teacher) session.getAttribute("teacher");
	}

	/**
	 * 初始化pageNo和pageSize。如果默认值有变更，统一在这里修改
	 */
	private Map<String, Integer> formatPageNoAndPageSize(Integer pageNo, Integer pageSize) {
		Map<String, Integer> map = new HashMap<>();
		if (pageNo == null || pageNo <= 0) {
			pageNo = 1;
		}
		if (pageSize == null || pageSize <= 0) {
			pageSize = 3;
		}
		map.put("pageNo", pageNo);
		map.put("pageSize", pageSize);
		return map;
	}

	/**
	 * 把查询结果集放入PageInfo中
	 * navigatePages 分页导航显示的页数
	 */
	public void pageIn(Model model, List list) {
		PageInfo page = new PageInfo(list, 3);
		model.addAttribute("pageInfo", page);
		System.err.println(page);
	}

}
